package com.jack.leetcode.eachProblem06;

public class 模式匹配 {

    public static void main(String[] args) {
        System.out.println(patternMatching("a", ""));
    }

    public static boolean patternMatching(String pattern, String value) {
        String[] s = new String[2];
        return solve(s, pattern, 0, value, 0);
    }

    private static boolean solve(String[] s, String pattern,int pIndex, String value, int vIndex) {
        if(pIndex == pattern.length() && vIndex == value.length()){
            return true;
        }
        if(pIndex >= pattern.length() || vIndex > value.length()){
            return false;
        }
        int num = pattern.charAt(pIndex) - 'a';
        if(s[num] == null){
            for(int i = vIndex; i <= value.length(); i++){
                s[num] = value.substring(vIndex, i);
                if(!s[num].equalsIgnoreCase(s[num^1]) && solve(s, pattern, pIndex+1, value, i)){
                    return true;
                }
            }
            s[num] = null;
            return false;
        }else{
            int end = vIndex + s[num].length();
            if(end > value.length() || !value.substring(vIndex, end).equalsIgnoreCase(s[num])){
                return false;
            }
            return solve(s, pattern, pIndex + 1, value, end);
        }
    }
}
